package com.lxb.blog.web.admin;

import com.lxb.blog.entity.Blog;
import com.lxb.blog.entity.User;
import com.lxb.blog.service.BlogService;
import com.lxb.blog.service.TagService;
import com.lxb.blog.service.TypeService;
import com.lxb.blog.vo.BlogQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/admin")
public class BlogController {

    @Autowired
    private BlogService blogService;
    @Autowired
    private TypeService typeService;
    @Autowired
    private TagService tagService;

    /**
     * 博客页面
     * @param pageable
     * @param blog
     * @param model
     * @return
     */
    @GetMapping("/blogs")
    public String blog(@PageableDefault(size = 4,sort = {"updateTime"},direction = Sort.Direction.DESC)
                                   Pageable pageable,
                       BlogQuery blog,
                       Model model){
        model.addAttribute("types",typeService.listType());
        model.addAttribute("page",blogService.listBlog(pageable,blog));
        return "admin/blogs";
    }

    /**
     * 搜索查询
     * @param pageable
     * @param blog
     * @param model
     * @return
     */
    @PostMapping("/blogs/search")
    public String search(@PageableDefault(size = 3,sort = {"updateTime"},direction = Sort.Direction.DESC)
                               Pageable pageable,
                       BlogQuery blog,
                       Model model){
        model.addAttribute("page",blogService.listBlog(pageable,blog));
        //返回给admin/blogs页面下blogList的片段
        return "admin/blogs :: blogList";
    }

    /**
     * 新增页面
     * @return
     */
    @GetMapping("/blogs/input")
    public String input(Model model){
        model.addAttribute("blog",new Blog());
        model.addAttribute("types",typeService.listType());
        model.addAttribute("tags",tagService.listTags());
        return "admin/blogs-input";
    }

    /**
     * 修改页面
     * @param model
     * @return
     */
    @GetMapping("/blogs/{id}/input")
    public String editInput(@PathVariable Integer id, Model model){
        Blog blog =blogService.getBlog(id);
        blog.init();
        model.addAttribute("blog",blog);
        model.addAttribute("types",typeService.listType());
        model.addAttribute("tags",tagService.listTags());
        return "admin/blogs-input";
    }

    /**
     * 新增/修改
     * @return
     */
    @PostMapping("/blogs")
    public String post(Blog blog, RedirectAttributes attributes, HttpSession session){

        blog.setUser((User)session.getAttribute("user"));
        //通过前端获取的type的id保存在blog的type对象里面
        blog.setType(typeService.getType(blog.getType().getId()));
        blog.setTags(tagService.listTags(blog.getTagIds()));
        Blog blog1= blogService.save(blog);
        if(blog1 == null){
            attributes.addFlashAttribute("message","操作失败");
            return "redirect:/admin/blogs";
        }
        attributes.addFlashAttribute("message","操作成功");
        return "redirect:/admin/blogs";
    }


    /**
     * 删除
     * @param id
     * @param attributes
     * @return
     */
    @GetMapping("/blogs/{id}/delete")
    public String delete(@PathVariable Integer id,RedirectAttributes attributes){
        if(id == null){
            attributes.addFlashAttribute("message","请联系开发人员");
            return  "redirect:/admin/blogs";
        }
        blogService.deleteBlog(id);
        attributes.addFlashAttribute("message","删除成功");
        return  "redirect:/admin/blogs";
    }
}
